package com.itany.crawler;

import com.itany.utils.Log;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Urls {
    public static String file = "data.txt";

    // 累计 爬取数量
    public int count = 0;

    public List<UrlBaseHandle> urls = new LinkedList<>();

    synchronized void add(UrlBaseHandle handle) {
        urls.add(0, handle);
    }

    synchronized void addLast(UrlBaseHandle handle) {
        urls.add(urls.size(), handle);
    }

    synchronized UrlBaseHandle pop() {
        return urls.remove(0);
    }


    public boolean isEmpty() {
        return urls.isEmpty();
    }

    public int size() {
        return urls.size();
    }

    public static void main(String[] args) {
        Urls urls = new Urls();

        urls.add(new IndexHandle());
        urls.add(new SecondHandle());
        urls.add(new ThirdHandle());


        //^http://www.bookschina.com

        urls.save();
        urls.load();
    }


    public void load() {
        try {
            List<String> list = Files.readAllLines(Paths.get(Main.testDataPath, Urls.file));
            list.forEach(url -> {
                if (!url.trim().isEmpty()) {
                    String degree = url.substring(0, 1);
                    String href = url.substring(1);
                    if (degree.equals("1")) {
                        IndexHandle handle = new IndexHandle(Main.enableProxy);
                        this.addLast(handle);
                    } else if (degree.equals("2")) {
                        SecondHandle handle = new SecondHandle(href, Main.enableProxy);
                        this.add(handle);
                    } else if (degree.equals("3")) {
                        ThirdHandle thirdHandle = new ThirdHandle(href, Main.enableProxy);
                        this.addLast(thirdHandle);
                    }
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void save() {
        try {
            Files.deleteIfExists(Paths.get(Main.testDataPath, Urls.file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < urls.size(); i++) {
            UrlBaseHandle handle = urls.get(i);
            if (handle instanceof IndexHandle) {
                Log.saveUrl(1 + handle.url);
            } else if (handle instanceof SecondHandle) {
                Log.saveUrl(2 + handle.url);
            } else if (handle instanceof ThirdHandle) {
                Log.saveUrl(3 + handle.url);
            }
        }
    }


}
